<?php
namespace Modules\Home\Validations;

use Phalcon\Validation\Validator\Digit;
use Phalcon\Validation\Validator\Uniqueness;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Confirmation;
use Phalcon\Validation\Validator\StringLength;

use Common\Models\UserModel;

class UserValidation extends \Phalcon\Validation {
  public function login($data) {
    $this -> rules('user_code',[
      new PresenceOf([
        'message' => '请输入会员编号!'
      ]),
      new Digit([
        'message' => '请输入正确会员编号!'
      ])
    ]);

    $this -> add('password',
      new PresenceOf([
        'message' => '请输入密码!'
      ])
    );

    $validationMessageGroup = $this -> validate($data);
    if($validationMessageGroup -> valid()) {
      return ['code' => -1, 'message' => $validationMessageGroup[0] -> getMessage()];
    }

    $findFirstUser = UserModel::findFirst('user_code = "' . $data['user_code'] . '"');
    if(!$findFirstUser || !$this -> security -> checkHash($data['password'], $findFirstUser -> password)) {
      return ['code' => -1, 'message' => '编号或密码错误!'];
    }

    if($findFirstUser -> status != 0) {
      return ['code' => -1, 'message' => $this -> config -> user_status[$findFirstUser -> status]];
    }

    return ['code' => 0, 'message' => '登陆成功!', 'user' => $findFirstUser];
  }

  public function create($post, $parameter) {
    $this -> add('service_user_id',
      new PresenceOf([
        'message' => '请输入服务中心!'
      ])
    );

    $this -> rules('user_code',[
      new PresenceOf([
        'message' => '请输入会员编号!'
      ]),
      new Uniqueness([
        'model' => new UserModel(),
        'message' => '您输入的会员编号已经存在!'
      ]),
      new Digit([
        'message' => '请输入数字'
      ]),
      new StringLength([
        'max' => 8,
        'min' => 5,
        'messageMaximum' => '请输入小于8位的数字',
        'messageMinimum' => '请输入大于5位的数字',
      ])
    ]);

    $this -> add('real_name',
      new PresenceOf([
        'message' => '请输入真实姓名!'
      ])
    );

    // 判断密码
    if(empty($post['user']['password']) || !isset($post['user']['password'])) {
      $post['user']['confirm_password'] = $post['user']['password'] = $this -> security -> hash('123456');
    } else {
      $post['user']['confirm_password'] = $post['user']['password'] = $this -> security -> hash($post['user']['password']);
    }
    // 判断安全密码
    if(empty($post['user']['safe_password']) || !isset($post['user']['safe_password'])) {
      $post['user']['confirm_safe_password'] = $post['user']['safe_password'] = $this -> security -> hash('123456');
    } else {
      $post['user']['confirm_safe_password'] = $post['user']['safe_password'] = $this -> security -> hash($post['user']['safe_password']);
    }

    $this -> add('password',
      new Confirmation([
        'message' => '您输入的密码不一致!',
        'with' => 'confirm_password'
      ])
    );

    $this -> add('safe_password',
      new Confirmation([
        'message' => '您输入的安全密码不一致!',
        'with' => 'confirm_safe_password'
      ])
    );

    $this -> add('phone',
      new PresenceOf([
        'message' => '请输入手机号!'
      ])
    );

    $this -> add('node_side',
      new PresenceOf([
        'message' => '请输入选择市场!'
      ])
    );

    $validate = $this -> validate($post['user']);
    if($validate -> valid()) {
      return ['code' => -1, 'message' => $validate[0] -> getMessage()];
    }

    // 判断会员编号
    $findFirstRegisterUser = UserModel::findFirst('user_code = "' . $post['user']['user_code'] . '"');
    if($findFirstRegisterUser) {
      return ['code' => -1, 'message' => '会员编号已经存在请修改!'];
    }

    // 判断推荐人
    $findFirstPromotionUser = UserModel::findFirst('user_code = "' . $post['promotion_user_code'] . '"');
    if(!$findFirstPromotionUser) {
      return ['code' => -1, 'message' => '推荐人不存在!'];
    }
    $post['user']['promotion_user_id'] = $findFirstPromotionUser -> user_id;

    // 判断店铺激活码
    $serviceUserWallet = (new \Modules\Home\Services\UserWalletService()) -> getOne($parameter['user']['user_id']);
    if($serviceUserWallet['code'] != 0 || $serviceUserWallet['user_wallet'] -> activation_code < 1) {
      return ['code' => -1, 'message' => '店铺激活码不足!'];
    }

    // 判断接点人
    $findFirstNodeUser = UserModel::findFirst('user_code = "' . $post['node_user_code'] . '"');
    if(!$findFirstNodeUser) {
      return ['code' => -1, 'message' => '接点人不存在!'];
    }
    // 判断左右区
    $findNodeUserSides = UserModel::find('node_user_id = ' . $findFirstNodeUser -> user_id);
    if($findNodeUserSides -> count() >= 2) {
      return ['code' => -1, 'message' => '您选择的接点人左右区已满,请选择其他接点人!'];
    }
    if($findNodeUserSides -> valid()) {
      if($findNodeUserSides -> getFirst() -> node_side == $post['user']['node_side']) {
        switch($post['user']['node_side']) {
          case '1':
            return ['code' => -1, 'message' => '您选择的接点人左区已满,请选择右区或其他接点人!'];
          case '2':
            return ['code' => -1, 'message' => '您选择的接点人右区已满,请选择左区或其他接点人!'];
        }
      }

      if(UserModel::findFirst('node_user_id = ' . $findNodeUserSides -> getFirst() -> user_id . ' AND node_side = ' . $post['user']['node_side'])) {
        return ['code' => -1, 'message' => '您注册的此节点已被注册,请更换接点人!'];
      }
    }

    if($post['province_code'] == 0) {
      $post['user_info']['address_code'] = 0;
    } else if($post['city_code'] == 0) {
      $post['user_info']['address_code'] = $post['province_code'];
    } else if($post['district_code'] == 0) {
      $post['user_info']['address_code'] = $post['city_code'];
    } else {
      $post['user_info']['address_code'] = $post['district_code'];
    }

    unset($post['district_code']);
    unset($post['city_code']);
    unset($post['province_code']);
    unset($post['node_user_code']);
    unset($post['promotion_user_code']);

    unset($post['user']['confirm_password']);
    unset($post['user']['confirm_safe_password']);

    $post['user']['node_user_id'] = $findFirstNodeUser -> user_id;
    $post['user']['is_reward'] = 0;
    $post['user']['status'] = 0;
    $post['user']['point_award'] = $parameter['USER_REGISTER_FIRST_POINT_REWARD']['value'];
    return ['code'=>0,'post'=>$post,'service_user_wallet'=>$serviceUserWallet['user_wallet']];
  }
}
?>